package com.wzcl.app.model.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;


@Data
@ApiModel(value = "统一返回对象")
public class RVO<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    /*返回状态码*/
    @ApiModelProperty(value = "返回状态码")
    private Integer code;
    /*返回提示信息*/
    @ApiModelProperty(value = "描述信息")
    private String message;
    /*返回数据*/
    @ApiModelProperty(value = "返回的数据")
    private T data;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @ApiModelProperty(value = "分页信息")
    private PageMassage page;

    public RVO() {
    }

    public RVO(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public RVO(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public RVO(Integer code, String message, T data, PageMassage page) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.page = page;
    }

    public RVO(Integer code, String message, T data, MyPage page) {
        this.code = code;
        this.message = message;
        this.data = data;
        PageMassage pageMassage = new PageMassage();
        pageMassage.setNumber(page.getPageNumber());
        pageMassage.setSize(page.getPageSize());
        pageMassage.setTotalElements(page.getTotalElements());
        pageMassage.setTotalPages(page.getTotalPages());
        this.page = pageMassage;
    }

    /**
     *快捷返回模板
     * @param data  需要返回的数据
     * @param <T>  泛型
     * @return RVO
     */
    public static <T> RVO<T> success(T data){
        RVO<T> rvo = new RVO<T>();
        rvo.setCode(CodeMsg.OK.getCode());
        rvo.setMessage(CodeMsg.OK.getMsg());
        rvo.setData(data);
        return rvo;
    }

    public static <T> RVO<T> success(String message, T data){
        RVO<T> rvo = new RVO<T>();
        rvo.setCode(CodeMsg.OK.getCode());
        rvo.setMessage(message);
        rvo.setData(data);
        return rvo;
    }

    public static <T> RVO<T> success(T data, MyPage page){
        RVO<T> rvo = new RVO<T>();
        rvo.code = CodeMsg.OK.getCode() ;
        rvo.message = CodeMsg.OK.getMsg();
        rvo.data = data;
        PageMassage pageMassage = new PageMassage();
        pageMassage.setNumber(page.getPageNumber());
        pageMassage.setSize(page.getPageSize());
        pageMassage.setTotalElements(page.getTotalElements());
        pageMassage.setTotalPages(page.getTotalPages());
        rvo.page = pageMassage;
        return rvo;
    }

    /**
     * 快捷返回模板，不携带数据版
     * @return
     */
    public static RVO<String> success(){
        RVO<String> rvo = new RVO<>();
        rvo.setCode(CodeMsg.OK.getCode());
        rvo.setMessage(CodeMsg.OK.getMsg());
        return rvo;
    }

    /**
     *快速返回错误
     * @param msg 错误信息
     * @return RVO
     */
    public static RVO<String> error(String msg){

        RVO<String> rvo = new RVO<>();
        rvo.setCode(500);
        rvo.setMessage(msg);
        return rvo;
    }

    /**
     *快速返回错误
     * @return RVO
     */
    public static RVO<String> error(CodeMsg codeMsg){
        RVO<String> rvo = new RVO<>();
        rvo.setCode(codeMsg.getCode());
        rvo.setMessage(codeMsg.getMsg());
        return rvo;
    }
}
